package com.lxl.bt.util;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;

import com.lxl.bt.R;

import java.text.DecimalFormat;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;


public class TextUtils {

    public static SpannableString typeface1(String text){


        SpannableString spannableString = new SpannableString(text);
        RelativeSizeSpan sizeSpan01 = new RelativeSizeSpan(1.0f);
        RelativeSizeSpan sizeSpan02 = new RelativeSizeSpan(0.75f);
        spannableString.setSpan(sizeSpan01, 0, 5, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(sizeSpan02, 6, text.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

        return spannableString;

    }
    public static String join(String split, List<String> strings){
        String ss="";
        for(String s:strings){
            ss=ss+split+s;
        }
        return ss;
    }
    public static SpannableString typeface2(String text0, String text1, int color){


        SpannableString spannableString = new SpannableString(text0+text1);
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(color);
        spannableString.setSpan(colorSpan, text0.length(), spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return spannableString;

    }
    public static SpannableString typeface3(String text0, String text1, String text2, int color1){


        SpannableString spannableString = new SpannableString(text0+text1+text2);
        ForegroundColorSpan colorSpan1 = new ForegroundColorSpan(color1);
        spannableString.setSpan(colorSpan1, text0.length(), text1.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

        return spannableString;

    }
    public static SpannableString typeface4(String text0, String rmb, String pricre, String text3, int color1, int color2){
        String tesxt = text0 + rmb + pricre + text3;
        RelativeSizeSpan sizeSpan01 = new RelativeSizeSpan(1.0f);
        RelativeSizeSpan sizeSpan02 = new RelativeSizeSpan(1.75f);
        SpannableString spannableString = new SpannableString(tesxt);
        ForegroundColorSpan colorSpan1 = new ForegroundColorSpan(color1);
        ForegroundColorSpan colorSpan2 = new ForegroundColorSpan(color2);
        spannableString.setSpan(colorSpan1,0, text0.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(sizeSpan01,0, text0.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

        spannableString.setSpan(colorSpan2,text0.length(), text0.length()+rmb.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(sizeSpan01,text0.length(), text0.length()+rmb.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

        spannableString.setSpan(colorSpan2,text0.length()+rmb.length(), text0.length()+rmb.length()+pricre.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(sizeSpan02,text0.length()+rmb.length(), text0.length()+rmb.length()+pricre.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

        spannableString.setSpan(colorSpan1,text0.length()+rmb.length()+pricre.length(), tesxt.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(sizeSpan01,text0.length()+rmb.length()+pricre.length(), tesxt.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return spannableString;

    }
    public static SpannableString typeface5(String text0, String text, int color1, int color2){
        String texts = text0 + text;

        SpannableString spannableString = new SpannableString(texts);
        ForegroundColorSpan colorSpan1 = new ForegroundColorSpan(color1);
        ForegroundColorSpan colorSpan2 = new ForegroundColorSpan(color2);
        spannableString.setSpan(colorSpan1,0, text0.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(colorSpan2,text0.length(),texts.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);



        return spannableString;

    }
    public static SpannableString typeface7(String text0, String text1, String text2){
//        String texts = text0 + text;

        SpannableString spannableString = new SpannableString(text0+text1+text2);


        RelativeSizeSpan sizeSpan01 = new RelativeSizeSpan(1.0f);
        RelativeSizeSpan sizeSpan02 = new RelativeSizeSpan(1.75f);

        spannableString.setSpan(sizeSpan01,0, text0.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(sizeSpan02,text0.length(),text0.length()+text1.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(sizeSpan01,text0.length()+text1.length(),text0.length()+text1.length()+text2.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);



        return spannableString;

    }

    public static SpannableStringBuilder typeface6(String text0, String text, int color1, int color2){

        SpannableStringBuilder spannable = new SpannableStringBuilder(text0+text);

        spannable.setSpan(new ForegroundColorSpan(color2),text0.length(),text0.length()+text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannable.setSpan(new ForegroundColorSpan(color1),0,text0.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        return  spannable;

    }
    /**
 * 保留小数点后2位
 *
 * @return
 */
public static String keepTwo(double b) {
    java.text.DecimalFormat format = new java.text.DecimalFormat("#0.00");
    String str = format.format(b);
    return str;
}
public static String keepTwo(String b) {
    if(android.text.TextUtils.isEmpty(b))
        return "0.00";
    java.text.DecimalFormat format = new java.text.DecimalFormat("#0.00");
    String str = format.format(Double.valueOf(b));
    return str;
}

    public static CharSequence setColor(Context context, String text, String text1) {
        SpannableStringBuilder style = new SpannableStringBuilder(text);
// 关键字“孤舟”变色，0-text1.length()
        style.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.color_bg_login)), 0, text1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return style;
    }
    public static CharSequence matcherSearchText(int color, String string, String keyWord) {
        SpannableStringBuilder builder = new SpannableStringBuilder(string);
        int indexOf = string.indexOf(keyWord);
        if (indexOf != -1) {
            builder.setSpan(new ForegroundColorSpan(color), indexOf, indexOf + keyWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return builder;
    }
    public static SpannableString matcherSearchText2(int color, String text, String keyword) {
        SpannableString ss = new SpannableString(text);

        Pattern pattern = Pattern.compile(keyword);
        Matcher matcher = pattern.matcher(ss);
        while (matcher.find()) {
            int start = matcher.start();
            int end = matcher.end();
            ss.setSpan(new ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return ss;
    }
    public static SpannableString matcherSearchText2(int color, int basecolor, String text, String keyword) {
        SpannableString ss = new SpannableString(text);
        ForegroundColorSpan colorSpan1 = new ForegroundColorSpan(basecolor);
        ss.setSpan(colorSpan1,0, text.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        Pattern pattern = Pattern.compile(keyword);
        Matcher matcher = pattern.matcher(ss);
        while (matcher.find()) {
            int start = matcher.start();
            int end = matcher.end();
            ss.setSpan(new ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return ss;
    }
    public static SpannableString matcherSearchTitle(int color, String text, String keyword) {
        String string = text.toLowerCase();
        String key = keyword.toLowerCase();
        Pattern pattern = Pattern.compile(key);
        Matcher matcher = pattern.matcher(string);
        SpannableString ss = new SpannableString(text);
        while (matcher.find()) {
            int start = matcher.start();
            int end = matcher.end();
            ss.setSpan(new ForegroundColorSpan(color), start, end,
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return ss;
    }

    /**
     * 半角字符与全角字符混乱所致：这种情况一般就是汉字与数字、英文字母混用将字符全角化
     **/
    public static String ToDBC(String input) {
        char[] c = input.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (c[i] == 12288) {
                c[i] = (char) 32;
                continue;
            }
            if (c[i] > 65280 && c[i] < 65375)
                c[i] = (char) (c[i] - 65248);
        }
        return new String(c);
    }
//商品数量格式化
    public static String getNumFormat(int goodNum) {
        if(goodNum<=0){
            return "";
        }else {
            return "x"+goodNum;
        }
    }
//价格格式化
    public static String getPriceFormat(double goodPrice) {
        DecimalFormat df = new DecimalFormat("#0.00");
       return "¥"+df.format(goodPrice);
    }
    public static String getPriceFormatPrinter(double goodPrice) {
        DecimalFormat df = new DecimalFormat("#0.00");
       return "￥"+df.format(goodPrice);
    }
//手机号隐藏中心几位
    public static String getPhoneFormart(String s) {
      return  s.replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2");
    }

    /**
     * 大陆号码或香港号码均可
     */
    public static boolean isPhoneLegal(String str) throws PatternSyntaxException {
        return isChinaPhoneLegal(str) || isHKPhoneLegal(str);
    }

    /**
     * 大陆手机号码11位数，匹配格式：前三位固定格式+后8位任意数
     * 此方法中前三位格式有：
     * 13+任意数
     * 145,147,149
     * 15+除4的任意数(不要写^4，这样的话字母也会被认为是正确的)
     * 166
     * 17+3,5,6,7,8
     * 18+任意数
     * 198,199
     */
    public static boolean isChinaPhoneLegal(String str) throws PatternSyntaxException {
        // ^ 匹配输入字符串开始的位置
        // \d 匹配一个或多个数字，其中 \ 要转义，所以是 \\d
        // $ 匹配输入字符串结尾的位置
        String regExp = "^((13[0-9])|(14[5,7,9])|(15[0-3,5-9])|(166)|(17[3,5,6,7,8])" +
                "|(18[0-9])|(19[8,9]))\\d{8}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.matches();
    }

    /**
     * 香港手机号码8位数，5|6|8|9开头+7位任意数
     */
    public static boolean isHKPhoneLegal(String str) throws PatternSyntaxException {
        // ^ 匹配输入字符串开始的位置
        // \d 匹配一个或多个数字，其中 \ 要转义，所以是 \\d
        // $ 匹配输入字符串结尾的位置
        String regExp = "^(5|6|8|9)\\d{7}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.matches();
    }


}
